package com.yangtao.framework.plugin.josql;

import org.josql.Query;
import org.josql.QueryExecutionException;
import org.josql.QueryParseException;
import org.josql.QueryResults;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象查询工具
 * Author: 杨涛
 * Date: 11-6-8
 * Time: 下午11:09
 */
public class ObjectQueryUtil {
	/**
	 * 根据指定的sql语句，查询对象
	 *
	 * @param data 数据集
	 * @param sql  sql
	 * @param <Y>  数据类型
	 * @return 符合条件的集合
	 */
	public static <Y> List<Y> queryForList(Collection<Y> data, String sql) {
		return queryForList(data, sql, new HashMap<String, Object>());
	}

	/**
	 * 根据制定的sql语句，查询对象
	 *
	 * @param data   数据集
	 * @param sql    sql
	 * @param values 查询参数
	 * @param <Y>    数据类型
	 * @return 符合条件的集合
	 */
	public static <Y> List<Y> queryForList(Collection<Y> data, String sql, Object... values) {
		Query query = new Query();
		try {
			query.parse(sql);
			for (int i = 0; i < values.length; i++) {
				query.setVariable(i + 1, values[i]);
			}
			QueryResults qr = query.execute(data);
			return qr.getResults();
		} catch (QueryParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		} catch (QueryExecutionException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
	}

	/**
	 * @param data
	 * @param sql
	 * @param params
	 * @param <Y>
	 * @return
	 */
	public static <Y> List<Y> queryForList(Collection<Y> data, String sql, Map<String, Object> params) {
		Query query = new Query();
		try {
			query.parse(sql);
			query.setVariables(params);
			QueryResults qr = query.execute(data);
			return qr.getResults();
		} catch (QueryParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		} catch (QueryExecutionException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
	}

	public static <Y> Y queryForObject(Collection<Y> data, String sql) {
		return queryForObject(data, sql, new HashMap<String, Object>());
	}

	/**
	 * @param data
	 * @param sql
	 * @param params
	 * @param <Y>
	 * @return
	 */
	public static <Y> Y queryForObject(Collection<Y> data, String sql, Object... params) {
		Query query = new Query();
		try {
			query.parse(sql);
			for (int i = 0; i < params.length; i++) {
				query.setVariable(i, params[i]);
			}
			QueryResults qr = query.execute(data);
			List<Y> result = qr.getResults();
			if (result != null && result.size() > 0) {
				return result.get(0);
			}
			return null;
		} catch (QueryParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		} catch (QueryExecutionException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
	}

	/**
	 * @param data
	 * @param sql
	 * @param params
	 * @param <Y>
	 * @return
	 */
	public static <Y> Y queryForObject(Collection<Y> data, String sql, Map<String, Object> params) {
		Query query = new Query();
		try {
			query.parse(sql);
			query.setVariables(params);
			QueryResults qr = query.execute(data);
			List<Y> result = qr.getResults();
			if (result != null && result.size() > 0) {
				return result.get(0);
			}
			return null;
		} catch (QueryParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		} catch (QueryExecutionException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
	}
}
